#pragma once

#include"ICallDispatcher.h"
#include"MemberFunctionPointer.h"
#include"XMLElement.h"
#include <map>
#include <string>

namespace XMLSAX
{
	namespace Common
	{
		template<class T>
		class __declspec(dllexport) FunctionContainer: public ICallDispatcher
		{
		private:
			std::map<std::wstring, MemberFunctionPointer<T> *> _internalDispatchMap;

		public:
			virtual void Dispatch(XMLElement *element)
			{
				if(_internalDispatchMap.count(element->Name()) != 0)
				{
					(*_internalDispatchMap[element->Name()])(element);
				}
			}

			void AddCallbackFunction(std::wstring key, MemberFunctionPointer<T> *pCallbackFcn)
			{
				_internalDispatchMap[key] = pCallbackFcn;
			}

			~FunctionContainer()
			{
				std::map<std::wstring, MemberFunctionPointer<T> *>::iterator pIterator = _internalDispatchMap.begin();

				while(pIterator != _internalDispatchMap.end())
				{
					delete pIterator->second;
					++pIterator;
				}
			}
		};
	}
}
